﻿Imports System.Data.OleDb
Public Class _3
    Inherits System.Web.UI.Page
    Public odConnection As OleDbConnection = New OleDbConnection()
    Public Shared idd As String

    Private Function Accsu(ByVal b As String, ByVal l As String, ByVal n As String， ByVal cl As String) As String
        Dim z As String
        '定义一个OLEDB连接并实例化它
        Dim con As New OleDbConnection
        '定义一个OLEDB命令并实例化他
        Dim cmd As New OleDbCommand
        '定义一个OLEDBReader方法来读取数据库
        Dim dr As OleDbDataReader
        '初始化con的连接属性，使用OLEDB模式，数据源为：你指定下路径，我的是在D盘
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\date\db\db.mdb"
        '打开OLEDB数据连接
        con.Open()
        '初始化OLEDB命令的连接属性为con,这个需要你理解下
        cmd.Connection = con
        '初始化OLEDB命令的语句 就是查询 什么字段从什么表 条件是ID等于你在t1中输入的内容
        cmd.CommandText = "select " & l & " from " & b & " where " & cl & "=" & n
        '执行OLEDB命令以ExecuteReader()方式，并返回一个OLEDBReader，赋值给dr
        dr = cmd.ExecuteReader()
        '判断下dr中是否有数据。如果有就把第一个值赋值给t2的值
        If dr.Read() Then
            z = dr(0)
            Accsu = z
        Else
            Response.Write("<Script Language=JavaScript>alert('未找到用户');</Script>")
            Accsu = ";"
        End If
        dr.Close()
        con.Close()
        '函数Accsu说明：b为查询的表，l为输出的列，n为查询列的值,cl为查询的列
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            idd = Session("ID").ToString()
        Catch
            Response.Write("<script>window.location='Default.aspx'</script>"）
        End Try
        If Accsu("nam", "f_jk", idd, "ID") = True Then
            Response.Write("<script>window.location='4.aspx'</script>"）
        End If
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim name As String = Accsu("nam", "nam", idd, "ID")
        Try
            If IsPostBack Then
                Dim Pfile As HttpPostedFile = Nothing
                For i = 0 To Request.Files.Count - 1
                    If Request.Files.Count > 0 Then
                        '获取上传的文件对象
                        Pfile = Request.Files(i)
                        '获取上传的文件完全限定名称(及包括后缀名)
                        Dim filename As String = Pfile.FileName
                        Dim xC As String = filename
                        xC = Strings.Right(xC, 3)
                        '获取上传文件的大小
                        Dim size As Int32 = Pfile.ContentLength
                        '设置存储路径
                        Dim path As String = "C:\date\image\" & name & "\"
                        '上传文件
                        If xC = "jpg" Or xC = "png" Or xC = "JPG" Or xC = "PNG" Then
                            Pfile.SaveAs(path & name & "父亲健康码." & xC)
                            'write db
                            odConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\date\db\db.mdb"
                            odConnection.Open()
                            Dim str As New System.Text.StringBuilder
                            str.Append("UPDATE nam SET nam.f_jk = True WHERE (([ID]=")
                            str.Append(idd)
                            str.Append("))")
                            Debug.WriteLine("")
                            Debug.WriteLine(str.ToString)
                            Dim cmdn As New OleDb.OleDbCommand(str.ToString, odConnection)
                            cmdn.ExecuteNonQuery()
                            odConnection.Close()

                            Dim all As Integer
                            all = Integer.Parse(Accsu("nam", "[all]", idd, "ID")) + 1
                            odConnection.Open()
                            Dim str1 As New System.Text.StringBuilder
                            str1.Append("UPDATE nam SET nam.[all] = '")
                            str1.Append(all & "' WHERE (([ID]=")
                            str1.Append(idd)
                            str1.Append("))")
                            Debug.WriteLine("")
                            Debug.WriteLine(str1.ToString)
                            Dim cmdn1 As New OleDb.OleDbCommand(str1.ToString, odConnection)
                            cmdn1.ExecuteNonQuery()
                            odConnection.Close()

                            Response.Write("<script type='text/javascript'>alert('提交成功')</script>")
                            Response.Write("<script>window.location='4.aspx'</script>"）
                        Else
                            Response.Write("<script type='text/javascript'>alert('禁止，请上传图片文件！')</script>")
                        End If
                    End If
                Next

            End If
        Finally
        End Try
    End Sub

    Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Response.Write("<script>window.location='4.aspx'</script>"）
    End Sub
End Class